<?php
class SecurityModel extends Model {
	protected $tableName = 'security';

	public function getList($post) {
		$map   = array();
		$order = 'id DESC';
		$limit = '20';
		$field = '*';

		//根据POST，组装查询条件
		if(!empty($post['id'])) {
			$map['id'] = array('in', $post['id']);
		}
		if(!empty($post['app'])) {
			$map['app'] = array('in', '"' . $post['app'] . '"');
		}
		if(!empty($post['mod'])) {
			$map['mod'] = array('in', '"' . $post['mod'] . '"');
		}
		if(!empty($post['act'])) {
			$map['act'] = array('in', '"' . $post['act'] . '"');
		}
		if(!empty($post['type']) && ($post['type'] == 'GET' || $post['type'] == 'POST')) {
			$map['type'] = $post['type'];
		}
		if(!empty($post['ignored']) && ($post['ignored'] == 0 || $post['ignored'] == 1)) {
			$map['ignored'] = $post['ignored'];
		}
		if(!empty($post['field']) && !empty($post['order'])) {
			$order = $post['field'] . ' ' . $post['order'];
		}
		if(!empty($post['limit'])) {
			$limit = intval($post['limit']);
		}

		$res = $this->where($map)->field($field)->order($order)->findPage($limit);
		return $res;
	}

	//导出excel报表
	public function outExcel(){
		require ADDON_PATH.'/libs/phpExcel/php-excel.class.php';
		$xls = new Excel_XML('UTF-8');

		$content = array(
			'0' => array('url','app','mod','act','type','参数名','详细参数'),
		);

		$res  = $this->findAll();
		//分组组装参数数组
		foreach($res as $k => $v) {
			$group[$v['group_flag']][$v['key']] = $v['value'];
		}

		foreach($res as $k => $v) {
			if(!isset($group[$v['group_flag']])) continue;

			$url = U($v['app'] . '/' . ucfirst($v['mod']) . '/' . $v['act']);
			foreach($group[$v['group_flag']] as $k2 => $v2) {
				$params[]		= $k2 . '=' . $v2;
				$param_names[]	= $k2;
			}
			$param = implode('&',$params);
			$v['type'] == 'GET' && $url .= '&' . $param;
			
			$content[] = array($url, $v['app'], $v['mod'], $v['act'], $v['type'], implode('/', $param_names), $param);
			unset($group[$v['group_flag']]);
			unset($params);
			unset($param_names);
			unset($param);
		}

		$xls->addArray($content);
		$xls->generateXML("security_".date('Y-m-d'));		
	}
}
?>